﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using ECCIT.Business.Model;
using ECCIT.Data;
using OgilvyOne.UKNow.Globalization;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.Model;

namespace OgilvyOne.UKNow.DAL
{
    public class MemberIntersetsMappingDAL : UKNowBaseDAL, IDALMemberIntersetsMapping
    {
        private const string TBL_NAME = "UKNOW_MEMBER_INTERESTS_MAPPING";
        private const string MEMBER_ID = "COL_MEMBER_ID";
        private const string COL_INTERESTS_ID = "COL_INTERESTS_ID";
        public override string TableName { get { return TBL_NAME; } }
        public override Type ModelType { get { return typeof(MemberIntersetsMappingModel); } }
       
        [PrimaryKey]
        public IField Memberid { get { return FieldFactory.Create(ModelType, "COL_MEMBER_ID", ColumnFactory.CreateColumn(MEMBER_ID, DataType.Int)); } }
        [PrimaryKey]
        public IField COL_FORM_ID { get { return FieldFactory.Create(ModelType, "COL_INTERESTS_ID", ColumnFactory.CreateColumn(COL_INTERESTS_ID, DataType.Int)); } }
        public override IField[] InsertFields { get { return new IField[] { Memberid, COL_FORM_ID }; } }
        public override IField[] SelectFields { get { return null; } }
        public override IField[] UpdateFields { get { return null; } }

         public MemberIntersetsMappingDAL() { }

         public MemberIntersetsMappingDAL(LanguageType languageType) { this.Language = LanguageFactory.Create(languageType); }

         public bool Save(IList<MemberIntersetsMappingModel> list)
         {
             if(list.Count>0)
             {
                 DeleteAll(list[0].COL_MEMBER_ID);
             }
             foreach (var memberIntersetsMappingModel in list)
             {
                 //Delete(memberIntersetsMappingModel);
                 Save(memberIntersetsMappingModel);

             }
             return true;
         }

         private void Save(MemberIntersetsMappingModel model)
         {
             Insert(model, null);
         }
         private void Delete(MemberIntersetsMappingModel model)
         {
             DeleteModelByPrimaryKey(this.GetType(),
                                     new KeyValuePair<IField, object>[]
                                         {
                                             new KeyValuePair<IField, object>(Memberid, model.COL_MEMBER_ID),
                                             new KeyValuePair<IField, object>(COL_FORM_ID, model.COL_INTERESTS_ID)
                                         });
         }

         public void DeleteAll(int memberid)
         {
             DBHelper.ExecuteNonQuery(
                 "delete from  UKNOW_MEMBER_INTERESTS_MAPPING where COL_MEMBER_ID=@memberid",
                 new[] {new SqlParameter("@memberid", SqlDbType.Int, 4) {Value = memberid}});
         }

    }
}
